package yxy.game.pm2.module.tower;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.towerlvreward;
import yxy.cherry.data.script.fomula;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;
import java.util.Collection;

public class TowerModule extends PlayerModule {
	// bean===========================================================================
	private int floor;// 完成的层数
	private int times;// 今天的挑战次数
	private int buytimes;// 今天的购买次数
	private DateTime time;// 时间标记

	private int[] angers;// 保存的怒气

	private ArrayList<Integer> levelReward = new ArrayList<>();

	public int getFloor() {
		return floor;
	}

	public void setFloor(int floor) {
		this.floor = floor;
	}

	public int getTimes() {
		return times;
	}

	public void setTimes(int times) {
		this.times = times;
	}

	public int getBuytimes() {
		return buytimes;
	}

	public void setBuytimes(int buytimes) {
		this.buytimes = buytimes;
	}

	public DateTime getTime() {
		return time;
	}

	public void setTime(DateTime time) {
		this.time = time;
	}

	public ArrayList<Integer> getLevelReward() {
		return levelReward;
	}

	public void setLevelReward(ArrayList<Integer> levelReward) {
		this.levelReward = levelReward;
	}

	public int[] getAngers() {
		return angers;
	}

	public void setAngers(int[] angers) {
		this.angers = angers;
	}

	// func===========================================================================

	/** 次数刷新 */
	public void updateTime() {
		DateTime now = DateTime.now();
		if (getTime() == null || !new LocalDate(now).equals(new LocalDate(getTime()))) {
			try {
				if (tryLock()) {
					setTime(now);
					setTimes(0);
					setBuytimes(0);
					setAngers(null);
					this.set();
				}
			} catch (Exception e) {
			} finally {
				unlock();
			}
		}
		check();
	}

	public int remainTimes() {
		int maxtimes = fomula.修炼塔每日挑战次数.intValue();
		int remain = maxtimes - getTimes();
		return remain;
	}

	public int remainBuyTimes() {
		int maxtimes = fomula.修炼塔每日购买次数.intValue();
		int remain = maxtimes - getBuytimes();
		return remain;
	}

	public void check() {
		{
			boolean tip = false;
			Collection<towerlvreward> datas = DataCenter.getAllData(towerlvreward.class).values();
			for (towerlvreward data : datas) {
				int condition = data.getCondition();
				if (condition > getFloor()) {
					continue;
				}
				if (getLevelReward().contains(Integer.valueOf(data.getId()))) {
					continue;
				}
				tip = true;
				break;
			}
			new TipHandle().updateTip(getPlayerCode(), TipType.主城_修炼塔_目标奖励, tip);
		}
		new TipHandle().updateTip(getPlayerCode(), TipType.主城_修炼塔_挑战, (remainTimes() > 0));
	}

	// redis==========================================================================
	private static String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:TOWER", playerCode);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public TowerModule get(String playerCode) {
		try {
			TowerModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), TowerModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}
			if (module == null) {
				module = create(playerCode);
			}
			if (module == null) return null;

			module.updateTime();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static private TowerModule create(String playerCode) {
		DateTime now = DateTime.now();
		TowerModule module = new TowerModule();
		module.setPlayerCode(playerCode);
		module.setFloor(0);
		module.setTime(now);
		module.setTimes(0);
		module.setBuytimes(0);
		module.set();
		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				TowerModule towerModule = getv(key(playerCode));
				if (towerModule != null) MySQLCacheDAO.setV(key, towerModule);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}
}
